
#include "user_ht16c22.h"
#include "string.h"

#define Mode_State     0x11
#define Flicker_State  0x12 

#define  Display_Head_Addr                0x13
#define  Display_Data_Len                 0x11

#define Line 10
#define Char_E 11
#define Char_L 12

uint8_t NUM_Library_1[12] = {0x5F,0x06,0x6B,0x2F,0x36,0x3D,0x7D,0x07,0x7F,0x3F,0x20,0x79};//数字0-9，- E
/*************************************************
  Function:       User_Long_to_Data
  Description:    长数据值转数组
  Input:          input 要转化的值
                  output 转化后的数组地址
                  len 长度
  Return:         无
  other：         123->[1]\[2]\[3];
*************************************************/
void User_Long_to_Data(uint32_t input,uint8_t* output,uint8_t len)
{
	uint8_t i;
	uint32_t input_copy;
	input_copy = input;
	for(i=0;i<len;i++)
	{
        output[len - i - 1] = input_copy % 10;
		input_copy = input_copy / 10;
	}
}

/*************************************************
  Function:       User_Ht16c22_Write_Data
  Description:    Ht16c22写协议
  Input:          info 显示结构体
                  len 数据长度
                  addr 写入地址
                  input 写入数据数组指针
  Return:         无
*************************************************/
void User_Ht16c22_Write_Data(Display_INFO* info,uint8_t len,uint8_t addr,uint8_t* input)
{
	uint8_t i;
	I2C_START(info->peripheral);
	I2C_Write_oneByte(info->peripheral,info->peripheral.ADDR);
	while(I2C_Check_ACK(info->peripheral))return;
	I2C_delay();
	I2C_Write_oneByte(info->peripheral,addr);
	while(I2C_Check_ACK(info->peripheral))return;
	I2C_delay();
	for(i=0;i<len;i++)
	{
		I2C_Write_oneByte(info->peripheral,input[i]);
		while(I2C_Check_ACK(info->peripheral))return;
		I2C_delay();
	}
	I2C_STOP(info->peripheral);
}

/*************************************************
  Function:       User_Display_Clear
  Description:    清除显示
  Input:          info 显示结构体
  Return:         无
*************************************************/
void User_Display_Clear(Display_INFO* info)
{
	uint8_t write_data[info->Data_Len];
	memset(&(info->Display_Show),0,sizeof(uint8_t)*(info->Data_Len));
    memset(write_data,0,sizeof(uint8_t)*(info->Data_Len));
	User_Ht16c22_Write_Data(info,info->Data_Len,info->Data_Head_Addr,write_data);
}

/*************************************************
  Function:       User_Ht16c22_Init
  Description:    显示模块初始化
  Input:          info 显示结构体
  Return:         无
*************************************************/
void User_Ht16c22_Init(Display_INFO* info)
{
	I2C_Peripheral_gpio_init(info->peripheral);
    info->Data_Head_Addr = Display_Head_Addr;
    info->Data_Len = Display_Data_Len;
    info->Show_World = true;
    User_Display_Clear(info);
    User_Ht16c22_Mode_Set(info,Mode_State);
	User_Ht16c22_Mode_Set(info,Flicker_State);
    
}

/*************************************************
  Function:       User_Ht16c22_Mode_Set
  Description:    Ht16c22模式设置
  Input:          info 显示结构体
                  Mode 设置模式
  Return:         无
*************************************************/
void User_Ht16c22_Mode_Set(Display_INFO* info,uint8_t Mode)
{
	I2C_START(info->peripheral);
	I2C_Write_oneByte(info->peripheral,info->peripheral.ADDR);
	while(I2C_Check_ACK(info->peripheral))return;
	I2C_delay();
	I2C_Write_oneByte(info->peripheral,Mode);
	while(I2C_Check_ACK(info->peripheral))return;
	I2C_delay();
	I2C_STOP(info->peripheral);
}

/*************************************************
  Function:       User_Display_Numset
  Description:    显示数字设定
  Input:          info 显示结构体
                  numbit 数字的位置
                  valֵ 要显示的值
  Return:         无   
*************************************************/
void User_Display_Numset(Display_INFO* info,uint8_t numbit,uint8_t val)
{
    switch(numbit)
    {
        case 1: info->Display_Show.num1 = NUM_Library_1[val];break;
        case 6: info->Display_Show.num6_high = (NUM_Library_1[val] >> 3);
                info->Display_Show.num6_low = (NUM_Library_1[val] & 0x07);break;
        default:
            break;
    }
}

/*************************************************
  Function:       User_Display_SetData
  Description:    
  Input:          info 显示结构体
                  type 设置类型
                  val  设置值
  Return:         无
*************************************************/
void User_Display_SetData(Display_INFO* info,Data_tpye type,uint16_t val)
{
    uint8_t Time_show[6];
    switch(type)
    {
        case Set_Time:
            info->Display_Data.Set_Time = val;
            break;
        default:
            break;
    }
    switch(type)
    {
        case Set_Time:
            info->Display_Show.counts_1 = 0;
            info->Display_Show.hours_3 = 0;
            if(info->Display_Data.Set_Time == 0)
            {
                info->Display_Show.colon = 0;
                User_Display_Numset(info,1,Line);
                User_Display_Numset(info,2,Line);
                User_Display_Numset(info,3,Line);
                User_Display_Numset(info,4,Line);
            }
            else
            {
                info->Display_Show.colon = 1;
                Time_show[0] = info->Display_Data.Set_Time / 60;
                User_Long_to_Data(Time_show[1],&Time_show[4],2);                    
                User_Display_Numset(info,4,Time_show[5]);
            }
            break;
        default:
            break;
    }
}


/*************************************************
  Function:       User_Display_Updata
  Description:    刷新显示数据
  Input:          info 显示结构体
  Return:         无
*************************************************/
void User_Display_Updata(Display_INFO* info)
{
    uint8_t send_data[info->Data_Len];
    memcpy(send_data,&(info->Display_Show),sizeof(uint8_t)*(info->Data_Len));
    User_Ht16c22_Mode_Set(info,Mode_State);
    FL_DelayMs(1);
	User_Ht16c22_Mode_Set(info,Flicker_State);
    FL_DelayMs(1);
    User_Ht16c22_Write_Data(info,info->Data_Len,info->Data_Head_Addr,send_data);
}
